package com.lyxy.controller;


import com.lyxy.pojo.Emp;
import com.lyxy.pojo.Result;
import com.lyxy.service.EmpService;
import com.lyxy.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Description TODO
 * 登录控制器
 */

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private EmpService empService;


    @PostMapping
    public Result login(@RequestBody Emp emp) {
        Emp user = empService.login(emp);
        if (user != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", user.getId());
            claims.put("username", user.getUsername());
            claims.put("name", user.getName());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        return Result.error("用户名或密码错误");

    }
}
